import request from '@/utils/request'
// crm商机操作接口
// 商机退回
export function optyReturn(id, continueCalls) {
  const data = {
    id,
    continueCalls
  }
  return request({
    url: '/call-center/api/v2/crm/opty/return',
    method: 'post',
    params: data,
    data
  })
}
export function optyReturnBatch(continueCalls, data) {
  return request({
    url: '/call-center/api/v2/crm/opty/returns',
    method: 'post',
    params: {
      continueCalls
    },
    data
  })
}
// 获取CRM地址级联信息，获取省级地址时传空
export function addrSubset(code = '') {
  return request({
    url: '/call-center/api/v2/crm/address/subset?code=' + code,
    method: 'get'
  })
}
// 获取经销商信息dealerName(经销商名称)必传
export function crmDealer(dealerName = '成都') {
  return request({
    url: '/call-center/api/v2/crm/dealer?dealerName=' + dealerName,
    method: 'get'
  })
}
// 根据客户ID获取CRM客户信息
export function optyCustomerData(id) {
  return request({
    url: '/call-center/api/v2/crm/opty/' + id,
    method: 'get'
  })
}
// 判断是否能合并商机accntPhoneOp(顾客电话)，id（商机id）；都为必传
export function checkMergeOpty(id, data) {
  return request({
    url: `/call-center/api/v2/crm/opty/checkMergeOpty/${id}`,
    method: 'post',
    data,
    params: data
  })
}
// 批量获取客户信息-CC客户详情页面-关联客户：查询CRM中电话号码相同的商机,以及接听状态为*的商机
// {accntPhoneOp:'顾客手机号(必传)',optyCode:'客户编码（商机编码）必传',clueCustomerLogo:''}
export function detailCorrelation(paramsData) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = []
  filtersRaw.push({ id: 'clueCustomerCode', property: 'clueCustomerCode', value: `[${paramsData.clueCustomerCode}]`, operator: 'not in' })
  filtersRaw.push({ id: 'phone', property: 'phone', value: paramsData.phone, operator: '=' })
  filtersRaw.push({ id: 'clueCustomerLogo', property: 'clueCustomerLogo', value: paramsData.clueCustomerLogo, operator: '=' })
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: '/call-center/api/v2/crm/opty/page',
    method: 'POST',
    data
  })
  // return request({
  //   url: '/call-center/api/v2/crm/opty/detail/correlation' + paramsForUrl(data),
  //   method: 'post',
  //   data
  // })
}
// 分配商机 CC系统点击【确认分配】或【仍然分配】时
// {belongOrg:'服务商编码(必传)',id:'商机ID(必传)'}
export function distributionDealer(id, data) {
  return request({
    url: '/call-center/api/v2/crm/opty/distributionDealer/' + id + paramsForUrl(data),
    method: 'post',
    data
  })
}
// 批量获取客户信息-CC客户编辑页面-关联客户：查询CRM中电话号码相同、客户状态为“正常”的客户
// {accntPhoneOp:'顾客手机号(必传)',optyCode:'客户编码（商机编码）必传',clueCustomerLogo:''}
export function getCorrelationData(data) {
  return request({
    url: '/call-center/api/v2/crm/opty/correlation' + paramsForUrl(data),
    method: 'post',
    data
  })
}
// 商机失效
// {failureCauses:'商机失效原因(必传)',id:'商机ID(必传)'}
export function optyFailure(id, data) {
  return request({
    url: `/call-center/api/v2/crm/opty/failure/${id}`,
    method: 'post',
    data,
    params: data
  })
}
// 客户匹配经销商(省市区必传)
export function matchBusiness(data) {
  return request({
    url: '/call-center/api/v2/crm/opty/matchBusiness',
    method: 'post',
    data
  })
}
// 合并商机
// {accntPhoneOp:'顾客手机号(必传)',id:'商机ID(必传)',relevanceId:'选择合并的商机ID(必传)'}
export function mergeOpty(id, data) {
  return request({
    url: '/call-center/api/v2/crm/opty/mergeOpty/' + id + paramsForUrl(data),
    method: 'post',
    data
  })
}
// 查询关联商机
export function optyRelation(id) {
  return request({
    url: '/call-center/api/v2/crm/opty/relation/' + id,
    method: 'get'
  })
}
// 客服商机报表
export function optyReport() {
  return request({
    url: '/call-center/api/v2/crm/opty/report',
    method: 'get',
    timeout: 10 * 60 * 1000
  })
}
// 保存商机
export function optySave(data) {
  return request({
    url: '/call-center/api/v2/crm/opty/save',
    method: 'post',
    data
  })
}
// 异常商机 CC系统确认【转为分配异常
// {id:'商机ID(必传)',unusualInfo :'异常原因(必传)'}
export function optyUnusual(id, params) {
  return request({
    url: `/call-center/api/v2/crm/opty/unusual/${id}`,
    method: 'post',
    params,
    data: params
  })
}
// 保存通话记录
export function recordSave(data) {
  return request({
    url: '/call-center/api/v2/crm/record/save',
    method: 'post',
    data
  })
}
// 查询crm通话记录(id:商机id)
export function recordList(id) {
  return request({
    url: '/call-center/api/v2/crm/record/list/' + id,
    method: 'get'
  })
}

export function paramsForUrl(params) {
  const strList = []
  let str = ''
  for (const i in params) {
    if (params[i] || params[i] === '0' || params[i] === 0) {
      strList.push(i + '=' + params[i])
    }
  }
  if (strList.length) {
    str = '?' + strList.join('&')
  }
  return str
}

// 商机查询
export function optyPage(data) {
  return request({
    url: `/call-center/api/v2/crm/opty/page`,
    method: 'POST',
    timeout: 10 * 60 * 1000,
    data
  })
}
/**
 * 获取预设经销商
 * @param {*} params
 * @returns
 */
export function preinstallDealer(params) {
  return request({
    url: '/call-center/api/v2/crm/recommendDealer',
    method: 'GET',
    params
  })
}
/**
 * 获取预设经销商
 * @param {*} data
 * @returns
 */
export function dealerMatchConfig(data) {
  return request({
    url: `/call-center/api/v2/crm/dealerMatchConfig/page`,
    method: 'POST',
    data
  }).then(res => {
    const data = res.data
    if (data.records && data.records.length > 0) {
      return data.records[0]
    }
    return Promise.reject(res)
  })
}
